<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>

<body>
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="" lay-verify="required" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">工号</label>
                        <div class="layui-input-block">
                            <input type="text" name="jobNumber" value="" lay-verify="required" autocomplete="off"
                                placeholder="请输入工号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="userName" value="" lay-verify="required" autocomplete="off"
                                placeholder="请输入姓名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="1" title="男">
                            <input type="radio" name="sex" value="0" title="女">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">职位</label>
                        <div class="layui-input-block">
                            <input type="text" name="job" value="" lay-verify="title" autocomplete="off"
                                placeholder="请输入职位" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">部门</label>
                        <div class="layui-input-block">
                            <input type="text" name="dept" value="" lay-verify="title" autocomplete="off"
                                placeholder="请输入部门" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" value="" lay-verify="title" autocomplete="off"
                                placeholder="请输入电话" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="password" value="" autocomplete="off" placeholder="请输入密码"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色</label>
                        <div id="test1" class="demo-transfer"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script src="../../../component/layui/layui.js"></script>
    <script src="../../../component/pear/pear.js"></script>

    <script>
        layui.use(['form', 'jquery', 'transfer', 'layer', 'util'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            var transfer = layui.transfer;
            var layer = layui.layer;
            var util = layui.util;

            // 从 URL 中获取 id 参数的值
            function getUrlParam(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }

            // 获取 id 值
            var id = getUrlParam('id');

            // 页面加载时向后端发送请求获取需要回显的数据
            $.ajax({
                url: 'http://localhost:8080/user_role/query?userId=' + id,
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('token', localStorage.getItem("token"));
                },
                success: function (res) {
                    var tempData = res.data;
                    var temp = [];
                    for (var i = 0; i < tempData.length; i++) {
                        temp[i] = tempData[i].roleId;
                    }

                    // 使用AJAX请求从后端API获取数据
                    $.ajax({
                        url: 'http://localhost:8080/role/query', // 替换成实际的后端API地址
                        type: 'GET',
                        headers: {
                            token: localStorage.getItem("token")
                        },
                        success: function (res) {
                            var resData = res.data;

                            // 构造符合data1格式的数组
                            var data1 = [];
                            for (var i = 0; i < resData.length; i++) {
                                var item = {
                                    "value": resData[i].id, // 将id转换为字符串赋值给value
                                    "title": resData[i].name // 将name赋值给title
                                };
                                data1.push(item);
                            }

                            layui.use('transfer', function () {
                                var transfer = layui.transfer;
                                transfer.render({
                                    elem: '#test1',
                                    title: ['请选择角色', '已选择'], //自定义标题
                                    data: data1,
                                    id: 'key123',
                                    value: temp,
                                    required: true
                                });
                            });
                        },
                    });


                }
            });





            // 页面加载时向后端发送请求获取需要回显的数据
            $.ajax({
                url: 'http://localhost:8080/user/query?id=' + id,
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('token', localStorage.getItem("token"));
                },
                success: function (res) {
                    $("input[name='id']").val(res.data[0].id);
                    $("input[name='jobNumber']").val(res.data[0].jobNumber);
                    $("input[name='userName']").val(res.data[0].userName);
                    $("input[name='sex'][value='" + res.data[0].sex + "']").prop('checked', true);
                    $("input[name='job']").val(res.data[0].job);
                    $("input[name='dept']").val(res.data[0].dept);
                    $("input[name='phone']").val(res.data[0].phone);
                    form.render();
                }
            });

            form.on('submit(user-save)', function (data) {
                var roleArray = transfer.getData('key123'); //获取右侧数据


                var data2 = [];
                for (var i = 0; i < roleArray.length; i++) {
                    var item = {
                        "userId": id,
                        "roleId": roleArray[i].value
                    };
                    data2.push(item);
                }


                var jsonData = JSON.stringify(data2); // 将roleArray转换为JSON字符串
                $.ajax({
                    url: 'http://localhost:8080/user_role/add',
                    data: jsonData,
                    contentType: 'application/json', // 设置内容类型为JSON
                    type: 'post',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('token', localStorage.getItem("token"));
                    },
                })


                $.ajax({
                    url: 'http://localhost:8080/user/update',
                    data: data.field,
                    type: 'post',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('token', localStorage.getItem("token"));
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 1000 });
                        }
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>